/*
HttpSharp: Modular HTTP server C# library targeting .NET/Mono
Copyright 2009 Thesis Kongpakdeepong

This file is part of HttpSharp.

HttpSharp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

HttpSharp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with HttpSharp.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using HttpSharp;

namespace HttpSharp.ConsoleServer
{
  public class ConsoleServer
  {
    private const ConsoleKey exitKey = ConsoleKey.End;
    private const ConsoleModifiers exitModifiers = ConsoleModifiers.Control | ConsoleModifiers.Shift;
    
    private static void WaitForExitKey()
    {
      Console.WriteLine("Press {0} and {1} to exit...", exitModifiers, exitKey);
      ConsoleKeyInfo keyPressed;
      do
      {
        keyPressed = Console.ReadKey(true);
      }
      while(!IsExitKey(keyPressed));
    }
    
    private static bool IsExitKey(ConsoleKeyInfo keyInfo)
    {
      return keyInfo.Key == exitKey && ((keyInfo.Modifiers & exitModifiers) == exitModifiers);
    }
    
    public static void Main(string[] args)
    {
      Server s = new Server();
      s.Ports = new int[] {80,443};
      s.OnProcessRequest += DumpConsole;
      s.Start();
      WaitForExitKey();
      s.Stop();
    }
    
    private static ProcessResults DumpConsole(HttpRequest request)
    {
      Console.Write(request.Header);
      foreach(RequestHeader key in request.Keys)
      {
        Console.WriteLine("{0}: {1}", key.ToString().PadLeft(15), request[key]);
      }
      return ProcessResults.Handled;
    }
  }
}